//提示输入
$(document).ready(function () {   
    $("#e-mail").blur(function () {
        $(".error").remove();
        var email = $("#e-mail").val();
        var Informations=localStorage.getItem("Informations");
        var informationData = JSON.parse(Informations);
        if (email == "") {
            var a = $(".tip-e-mail").text();
            $(".tip-e-mail").html("<span class='error'>邮箱不能为空!</span>");
            return false;
        } else {
            if (!(email.indexOf("@") > 0 && email.indexOf(".com") > 0)) {
                $(".tip-e-mail").html("<span class='error'>请输入正确的邮箱！</span>");
                return;
            }
            for(var i = 0; i < informationData.length; i++){
            if(informationData[i].email==email){
               $(".tip-e-mail").html("<span class='error'>该邮箱已经注册！</span>")
               return false;
            }
            }
        }
    });
    $("#id").blur(function (information) {
        $(".error").remove();
        var id = $("#id").val();
        if (id == "") {
            var a = $(".tip-id").text();
            $(".tip-id").html("<span class='error'>昵称不能为空!</span>");
            return false;
        }
    });
    $("#password").blur(function () {
        $(".error").remove();
        var password = $("#password").val();
        if (password == "") {
            var a = $(".tip-password").text();
            $(".tip-password").html("<span class='error'>密码不能为空!</span>");
            return false;
        } 
    });
    $("#password-sure").bind('input porpertychange',function () {
         $(".error").remove();
        var password = $("#password").val();
        var surePassword = $("#password-sure").val();
        if(password!=surePassword){
            var a = $(".tip-sure").text();
            $(".tip-sure").html("<span class='error'>两次密码不同！</span>");
            return false;
        }else{
            var email = $("#e-mail").val();
            var id = $("#id").val();
            var password = $("#password").val();
            if (email && id && password != null) {
                $("button").attr("disabled", false);
                return ;
            }
        }
    });
    //注册
    $("#registerBtn").on("click",function(){
       id=$("#id").val();
       username=$("#e-mail").val();
       password=$("#password").val();
       passwordsure=$("#password-sure").val();      
       var information={"id":id,"email":username,"password":password};
       var data=getData();
       data.push(information);
       saveData(data);
       alert("注册成功");
       location.href="ToDoList-login.html";
       window.event.returnValue=false;   
});
    //登录
  $("#login").on("click",function(){
    username=$("#name").val();
    password=$("#inputpassword").val();
    var information=localStorage.getItem("Informations");
    var informationData = JSON.parse(information);
    for (var i = 0; i < informationData.length; i++) {
        if(informationData[i].email==username&&informationData[i].password==password){
            alert("登录成功");
            location.href="ToDoList-work.html?username="+username+"";
            return;
        }
    }
    alert("账号或者密码错误！");
});
  
});

function getData() {
    var data=localStorage.getItem("Informations");
    if(data!=null){
        return JSON.parse(data);
    }
    else{
        return [];
    }

}

function saveData(data){
    localStorage.setItem("Informations",JSON.stringify(data));
}
//删除
function deleteUser(){
    var data=getData();
    for(var i = 0;i < data.length;i++){
        data.splice(i,1);
        saveData(data);
    }    
    
}
